package com.cetcs.kmga.dataManager.controller;

import com.cetcs.kmga.common.*;
import com.cetcs.kmga.dataManager.annotation.LogAnnotation;
import com.cetcs.kmga.dataManager.entity.dataManager.ClientDB;
import com.cetcs.kmga.dataManager.entity.dataManager.vi.ClientDBVi;
import com.cetcs.kmga.dataManager.entity.dataManager.vi.UpdateClientDBVi;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.ClientDBVo;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.ClientForDBVo;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.MultiLineChartVo;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.HistogramChartVo;
import com.cetcs.kmga.dataManager.entity.global.LogLevelEnum;
import com.cetcs.kmga.dataManager.entity.global.LogTypeEnum;
import com.cetcs.kmga.dataManager.service.dataManager.TrafficStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import javax.jws.WebResult;
import java.util.List;

/**
 * 流量分析.流量概览页面控制器
 * Created by root on 2017/9/21.
 */
@RestController
@RequestMapping("/trafficStatistic")
public class TrafficStatisticController extends BaseController {

    @Resource(name="trafficStatisticRealService")
    private TrafficStatisticService trafficStatisticService;

    /**
     * 查询资源的流量概览
     * @return
     */
    @LogAnnotation(module = "数据监管-流量分析-数据库流量", logType = LogTypeEnum.业务日志, description = "查询资源的流量概览", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findResourceTraffic", method = RequestMethod.GET)
    public BaseResponse findResourcetraffic(@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(value = "keyword", required = false) String keyword,
                                            @RequestParam(value = "id", required = false) String id) {
        try {
            Page result = trafficStatisticService.findResourcetraffic(pageNumber,pageSize,keyword,id);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 流量统计概览
     * @return
     */
    @LogAnnotation(module = "数据监管-流量分析-数据库流量", logType = LogTypeEnum.业务日志, description = "流量统计概览", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findTotalStatistic", method = RequestMethod.GET)
    public BaseResponse findTrafficStatistic(@RequestParam(value = "id",required = false) Integer resourceId) {
        try {
            List<HistogramChartVo> result = trafficStatisticService.findTrafficStatistic(resourceId);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 访问量统计
     * @return
     */
    @LogAnnotation(module = "数据监管-流量分析-数据库流量", logType = LogTypeEnum.业务日志, description = "访问量统计", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findAccessAmount", method = RequestMethod.GET)
    public BaseResponse findAccessAmount(@RequestParam(value = "timeCycle", defaultValue = "1") int timeCycle,
                                         @RequestParam(value = "id",required = false) Integer resourceId) {
        try {

            MultiLineChartVo result = trafficStatisticService.findAccessAmount(timeCycle,resourceId);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 访问次数统计
     * @return
     */
    @LogAnnotation(module = "数据监管-流量分析-数据库流量", logType = LogTypeEnum.业务日志, description = "访问次数统计", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findAccessTimes", method = RequestMethod.GET)
    public BaseResponse findAccessTimes(@RequestParam(value = "timeCycle", defaultValue = "1") int timeCycle,
                                        @RequestParam(value = "id",required = false) Integer resourceId) {
        try {

            MultiLineChartVo result = trafficStatisticService.findAccessTimes(timeCycle,resourceId);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 查询资源的访问客户端IP列表
     */
    @LogAnnotation(module = "数据监管-流量分析-数据库流量", logType = LogTypeEnum.业务日志, description = "查询资源的访问客户端IP列表", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findIpOfResource", method = RequestMethod.GET)
    public BaseResponse findIpOfResource(  @RequestParam(value = "id", required = false) int id,
                                           @RequestParam(value = "type", required = false) String type,
                                           @RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestParam(value = "keyword", required = false) String keyword) {
        try {
            Page result = trafficStatisticService.findIpOfResource(id,type, pageNumber, pageSize, keyword);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**=================================yyangs start=========================**/

    /**************
     * 客户端管理**
     * ***********
     */

    /**
     * 获取客户端列表（合法和未知）
     */
    @LogAnnotation(module = "数据监管-流向分析-客户端管理", logType = LogTypeEnum.业务日志, description = "获取客户端列表（合法和未知）", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getClientListForDatabaseDirectConnection", method = RequestMethod.GET)
    public BaseResponse getClientListForDatabaseDirectConnection(WebRequest request) {
        try {
            Data dataMap = getParam(request);
            dataMap.put("cluster_id_null",1);
            ClientForDBVo clientForDBVo = trafficStatisticService.getClientListForDatabaseDirectConnection(dataMap);
            return this.ok(clientForDBVo);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }
    /**
     * 修改客户端信息
     */
    @LogAnnotation(module = "数据监管-流向分析-客户端管理", logType = LogTypeEnum.业务日志, description = "修改客户端信息", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/updateClientInfo", method = RequestMethod.POST ,produces = {"application/json", "application/xml"})
    public BaseResponse updateClientInfo(@RequestBody ClientDBVi clientDBVi, BindingResult bindingResult) {
        try {
            Data dataMap = new Data();
            dataMap.put("ip",clientDBVi.getId().substring(1));
            dataMap.put("name",clientDBVi.getName());
            dataMap.put("remark",clientDBVi.getText());
            Integer result = trafficStatisticService.updateClientInfo(dataMap);
            if(result>0) {
                return this.ok(result);
            }else{
                return this.error(Constant.SYSTEM_EXCEPTION,"名称已存在，请改用其他名称。");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }
    /**
     * 拖拽客户端
     */
    @LogAnnotation(module = "数据监管-流向分析-客户端管理", logType = LogTypeEnum.业务日志, description = "拖拽客户端", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/updateClientInfoFromOther", method = RequestMethod.POST)
    public BaseResponse updateClientInfoFromOther(@RequestBody UpdateClientDBVi updateClientDBVi, BindingResult bindingResult) {
        try {
            Data dataMap = new Data();
            dataMap.put("ip",updateClientDBVi.getId().substring(1));
            dataMap.put("type_code",updateClientDBVi.getToTerminal().substring(1));
            if("A0".equals(updateClientDBVi.getFromTerminal())){
                dataMap.put("deal_flag",1);
            }
            Integer result = trafficStatisticService.updateClientInfo(dataMap);
            if(result>0) {
                return this.ok(result);
            }else{
                return this.error(Constant.SYSTEM_EXCEPTION);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 查询客户端详情 根据ID
     */
    @LogAnnotation(module = "数据监管-流向分析-客户端管理", logType = LogTypeEnum.业务日志, description = "查询客户端详情", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getClientInfoById/{ids}", method = RequestMethod.GET)
    public BaseResponse getClientInfoById(@PathVariable("ids") String ids,
                                          @RequestParam(value = "id")String id) {
        try {
            Data dataMap = new Data();
            dataMap.put("ip",id.substring(1));
            ClientDB clientDB = trafficStatisticService.getClientInfoById(dataMap);
            ClientDBVo clientDBVo = Client2ClientVo(clientDB);
            return this.ok(clientDBVo);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


    /**
     * 客户端转化为Vo
     * @param clientDB
     * @return
     */
    public static ClientDBVo Client2ClientVo(ClientDB clientDB){
        ClientDBVo clientDBVo = new ClientDBVo();
        clientDBVo.setId(clientDB.getIp());
        clientDBVo.setIp(clientDB.getIp());
        clientDBVo.setFromId(clientDB.getCluster_id());
        clientDBVo.setName(clientDB.getName());
        clientDBVo.setText(clientDB.getRemark());
        return clientDBVo;
    }


    /**
     * 拖拽客户端
     */
    @LogAnnotation(module = "数据监管-流向分析-客户端管理", logType = LogTypeEnum.业务日志, description = "拖拽客户端", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/updateClientStatusFromOther", method = RequestMethod.GET)
    public BaseResponse updateClientStatusFromOther(@RequestParam(value = "id")String id) {
        try {
            Data dataMap = new Data();
            dataMap.put("ip",id.substring(1));
            dataMap.put("deal_flag",1);
            Integer result = trafficStatisticService.updateClientInfo(dataMap);
            if(result>0) {
                return this.ok(result);
            }else{
                return this.error(Constant.SYSTEM_EXCEPTION);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }




    /**=================================yyangs end=========================**/


}
